package com.ghn.cocknovel.ui.activity.map.vm

import android.app.Application
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.basemodel.base.BaseViewModel
import com.ghn.cocknovel.constants.Constants
import com.ghn.cocknovel.net.DataService
import com.ghn.cocknovel.ui.activity.main.vm.MainViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kt.NetworkModel.bean.BQueryDriverTransitOrderListBean
import com.kt.NetworkModel.bean.BqueryBean
import com.kt.NetworkModel.bean.QueryPassengerBySiteBean
import com.kt.NetworkModel.bean.QueryTransitFenceAreaCoordinateBean
import com.kt.NetworkModel.bean.QueryTransitPromptBean
import com.kt.NetworkModel.bean.QueryTransitRouteBean
import com.kt.NetworkModel.bean.ResultListBean
import com.kt.NetworkModel.bean.TransitArriveStartingPointBean
import com.kt.NetworkModel.net.CallBack
import com.kt.NetworkModel.net.RxUtils

/**
 * @author 浩楠
 *
 * @date 2023/10/16-20:32.
 *
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * @Description: TODO
 */
open class MapViewModel(application: Application) : BaseViewModel(application) {
    companion object {
        val TAG: String? = MainViewModel::class.simpleName
    }

    val queryPassengerBySite = MutableLiveData<QueryPassengerBySiteBean?>()
    val queryTransitFenceAreaCoordinate = MutableLiveData<MutableList<QueryTransitFenceAreaCoordinateBean>?>()
    val queryDriverTransitOrderList = MutableLiveData<BqueryBean<BQueryDriverTransitOrderListBean>>()
    val transitArriveStartingPoint = MutableLiveData<String>()
    val onTransit = MutableLiveData<String?>()
    val onTransits = MutableLiveData<TransitArriveStartingPointBean?>()
    val allqueryDriverTransitOrderList = MutableLiveData<BqueryBean<BQueryDriverTransitOrderListBean>>()
    val cancelTimeoutOrder = MutableLiveData<String?>()
    val updateDriverNextSite = MutableLiveData<String?>()
    val updateDriverNextSiteError = MutableLiveData<Int>()
    val transitArriveStartingPointError = MutableLiveData<Int>()
    val arriveTransit = MutableLiveData<String>()
    val mQueryTransitPromptBeanLiveData = MutableLiveData<QueryTransitPromptBean>()
    val mUpdateDiverReceiveOrderLiveData = MutableLiveData<Int>()
    val queryTransitRouteBeanLiveData = MutableLiveData<MutableList<QueryTransitRouteBean>>()
    val queryOverTimeLiveData = MutableLiveData<Long>()

    /**
     * 任务详情页查询任务信息
     */
    open fun queryTransitOrder() {
        DataService.queryPassengerBySite().compose(RxUtils.mainSync()).subscribe(object : CallBack<QueryPassengerBySiteBean>() {
                override fun onResult(result: QueryPassengerBySiteBean?) {
                    result?.also {
                        queryPassengerBySite.value = it
                    }
                }
            })
    }

    /**
     * 查询区域坐标集合
     */
    open fun queryTransitFenceAreaCoordinate(fenceAreaId: String) {
        DataService.queryTransitFenceAreaCoordinate(fenceAreaId).compose(RxUtils.mainSync()).subscribe(object : CallBack<MutableList<QueryTransitFenceAreaCoordinateBean>>() {
                override fun onResult(result: MutableList<QueryTransitFenceAreaCoordinateBean>?) {
                    result?.also {
                        queryTransitFenceAreaCoordinate.value = it
                    }
                }

            })
    }

    /**
     * 司机端订单列表查询
     */
    open fun squeryDriverTransitOrderList(siteId: String, operate: Int, taskId: String) {
        DataService.squeryDriverTransitOrderList(siteId, operate, taskId).compose(RxUtils.mainSync()).subscribe(object : CallBack<BqueryBean<BQueryDriverTransitOrderListBean>>() {
                override fun onResult(result: BqueryBean<BQueryDriverTransitOrderListBean>?) {
                    result?.also {
                        queryDriverTransitOrderList.value = it
                    }
                }

            })
    }

    /**
     * 司机端订单列表查询全部列表查询
     */
    open fun AllsqueryDriverTransitOrderList(siteId: String, taskId: String) {
        DataService.AllsqueryDriverTransitOrderList(siteId, taskId).compose(RxUtils.mainSync()).subscribe(object : CallBack<BqueryBean<BQueryDriverTransitOrderListBean>>() {
                override fun onResult(result: BqueryBean<BQueryDriverTransitOrderListBean>?) {
                    result?.also {
                        allqueryDriverTransitOrderList.value = it
                    }
                }

            })
    }

    /**
     * 司机到达站点
     */
    open fun transitArriveStartingPoint(siteId: Int, carLatitude: String, carLongitude: String) {
        DataService.transitArriveStartingPoint(siteId, carLongitude, carLatitude).compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {
                    result?.also {
                        transitArriveStartingPoint.value = it
                        //发送到达站点消息
                        LiveEventBus.get<String>(Constants.EventBus.DRIVER_TO_SITE).post("")
                    }
                }

                override fun onError(e: String, code: Int): Boolean {
                    transitArriveStartingPointError.value = code
                    Log.i(TAG, "onError: $e")
                    return super.onError(e, code)
                }

            })
    }

    /**
     * 确认上车
     */
    open fun getOnTransit(orderId: String, siteId: Int, carLatitude: String, carLongitude: String) {
        DataService.getOnTransit(orderId, siteId, carLongitude, carLatitude).compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {
                    result?.also {
                        onTransit.value = it
                    }
                }

                override fun onError(e: String, code: Int): Boolean {
                    Log.i(TAG, "onError: ${e.toString()}")
                    onTransit.value = e
                    return super.onError(e, code)
                }

            })
    }

    /**
     * 用户超时未上车司机取消订单
     */
    open fun cancelTimeoutOrder(orderId: String) {
        DataService.cancelTimeoutOrder(orderId).compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {
                    result?.also {
                        cancelTimeoutOrder.value = it
                    }
                }

            })
    }

    /**
     * 司机端实时记录车辆位置
     */
    open fun saveTransitPositionInfo(carLatitude: String, carLongitude: String) {
        DataService.saveTransitPositionInfo(carLatitude, carLongitude).compose(RxUtils.mainSync()).subscribe(object : CallBack<TransitArriveStartingPointBean>() {
                override fun onResult(result: TransitArriveStartingPointBean?) {
                    result?.also {
                        onTransits.value = it
                    }
                }

            })
    }

    /**
     * 继续行程
     */
    open fun updateDriverNextSite(siteId: String, carLatitude: String, carLongitude: String) {
        DataService.updateDriverNextSite(siteId, carLatitude, carLongitude).compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {
                    updateDriverNextSite.value = result
                }

                override fun onError(e: String, code: Int): Boolean {
                    updateDriverNextSiteError.value = code
                    return super.onError(e, code)
                }
            })
    }

    /**
     * 继续行程
     */
    open fun arriveTransit(orderId: String, siteId: String, carLatitude: String, carLongitude: String) {
        DataService.arriveTransit(orderId, siteId, carLatitude, carLongitude).compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {
//                    Log.i(TAG, "onResult: $result")
                }

                override fun onError(e: String, code: Int): Boolean {
//                    Log.i(TAG, "onError: $e")
                    arriveTransit.value = e
                    return super.onError(e, code)
                }

            })
    }

    /**
     * 查询司机提示语【司机点弹窗触发后台关闭弹窗接口 后台设置关闭】【需要定时任务定时查询】
     */
    open fun queryTransitPromptNet() {
        DataService.queryTransitPromptNet().compose(RxUtils.mainSync()).subscribe(object : CallBack<QueryTransitPromptBean>() {
                override fun onResult(result: QueryTransitPromptBean?) {
                    result?.also {
                        mQueryTransitPromptBeanLiveData.value = it
                    }
                }

            })
    }

    /**
     *司机暂停或重新开始派单
     * 1正常接单【如果司机此刻是暂停派单状态显示】 2暂停接单【如果司机此刻是正常接单状态显示】
     */
    open fun updateDiverReceiveOrderNet(canReceiveOrder: Int) {
        DataService.updateDiverReceiveOrderNet(canReceiveOrder.toString()).compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {
                    mUpdateDiverReceiveOrderLiveData.value = canReceiveOrder
                }

            })
    }

    /**
     * 查询司机后续路径站点信息【不包括当前的站点 当前站点还是从查询任务信息里面拿的】
     */
    open fun queryTransitRouteNet() {
        DataService.queryTransitRouteNet().compose(RxUtils.mainSync()).subscribe(object : CallBack<MutableList<QueryTransitRouteBean>>() {
                override fun onResult(result: MutableList<QueryTransitRouteBean>?) {
                    result?.also {
                        queryTransitRouteBeanLiveData.value = it
                    }
                }
            })
    }

    /**
     *  关闭弹窗提示
     */
    fun updateTransitPromptNet() {
        DataService.updateTransitPromptNet().compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {

                }

                override fun onError(e: String, code: Int): Boolean {
                    return true
                }
            })
    }


    fun getSiteCountdown(siteId: String) {
        DataService.getSiteCountdown(siteId).compose(RxUtils.mainSync()).subscribe(object : CallBack<String>() {
                override fun onResult(result: String?) {
                    result?.also {
                        try {
                            queryOverTimeLiveData.value = result.toLong()
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            })
    }
}